/* 
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license
 */

var Peopletab = {
	init: function() {
		$("#Register").bind("click", function(){Peopletab.modal.initModal(
			{modal: true, 
			 width: 500, 
			 height: 450,
			 modalID: $(this).attr("id"),
			 ajax: $(this).attr("href")}
		); return false});
		
	    $("#ownerLogin").bind("click", function(){Peopletab.modal.initModal(
			{modal: true, 
			 width: 350, 
			 height: 250,
			 modalID: $(this).attr("id"),
			 ajax: $(this).attr("href")}
		); return false});
			
		$('#bd.profile').tabs({ 
		 	remote: true,
			bookmarkable: true,
			fxFade: true,
   			fxSpeed: 'fast',
			navClass: 'ajaxTabs',
			selectedClass: 'active',
			containerClass: 'ajaxContainer',
			onHide: Peopletab.tabs.hideMain,
			onShow: Peopletab.tabs.loadAjax
		});
		
		$.extend($.fn.Tooltip.defaults, {
			track: true,
			delay: 0,
			showURL: false,
			showBody: " - "
		});
		
		$('#identity').Tooltip();
	},
	
	modal: { 
		initModal: function(o) {
			o.modalID = o.modalID + "Modal";
			// if ID
			if (!$("#" + o.modalID).length) {
				var _o = {
					width: 600,
					height:480,
					trigger: false,
					modalClass : 'jqmWindow'
				};
				var options = $.extend(_o, o);
				if (options.ajax) {
					options.onLoad = Peopletab.modal.loadAjax;
					
				}
				var content;
				if (!options.content) {
					content = 'Please wait... <img src="/file/peopletab/image/loadingAnimation.gif" alt="loading" />'
				} else {
					content = options.content;
				}
				$("body").append('<div class="' + options.modalClass +'" id="'+ options.modalID +'">'+ content +'</div>');
				$("#" + options.modalID).jqm(options);
				TB_Position();
				$(window).resize(TB_Position);
			    $(window).scroll(TB_Position);
				
				// Show the modal
				$("#" + options.modalID).jqmShow();
				
				
			// else just show modal
			} else {
				if (o.ajax) {
					$("#" + o.modalID).html('Please wait... <img src="/file/peopletab/image/loadingAnimation.gif" alt="loading" />');
				}
				$("#" + o.modalID).html();
				$("#" + o.modalID).jqmShow();
				
			}
			
			// Taken from thickbox
			function TB_Position(){
				var pagesize = TB_getPageSize();  
				var arrayPageScroll = TB_getPageScrollTop();
				var style = {width: options.width, left: (arrayPageScroll[0] + (pagesize[0] - options.width)/2), top: (arrayPageScroll[1] + (pagesize[1]-options.height)/2.2)};
				$("#" + options.modalID).css(style);
			}
			function TB_getPageScrollTop(){
			  var yScrolltop;
			  var xScrollleft;
			  if (self.pageYOffset || self.pageXOffset) {
			    yScrolltop = self.pageYOffset;
			    xScrollleft = self.pageXOffset;
			  } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){   // Explorer 6 Strict
			    yScrolltop = document.documentElement.scrollTop;
			    xScrollleft = document.documentElement.scrollLeft;
			  } else if (document.body) {// all other Explorers
			    yScrolltop = document.body.scrollTop;
			    xScrollleft = document.body.scrollLeft;
			  }
			  arrayPageScroll = new Array(xScrollleft,yScrolltop) 
			  return arrayPageScroll;
			}
			function TB_getPageSize(){
			  var de = document.documentElement;
			  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
			  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
			  arrayPageSize = new Array(w,h) 
			  return arrayPageSize;
			}
		},
		
		loadAjax: function(hash) {
			// Forms
			$("form",this).ajaxForm({
				target:"#" + hash.c.modalID,
				success: function(){Peopletab.modal.loadAjax(hash)}
			}); 
			// Links
			$("#" + hash.c.modalID + " a").bind("click", function(){
			  $("#" + hash.c.modalID).load($(this).attr("href"), function(){Peopletab.modal.loadAjax(hash)});
			  return false;
			});
			// Close Links
			hash.w.jqmAddClose(".jqmClose");
			return false;
		},
		
		closeWatch: function() {
			$("watchModal").jqmHide();
			// Refresh page
			window.location.replace(  window.location.href );
		}
		
		
	},
	
	tabs: {
		loadAjax: function(t,el) {
			
			//$("form",el).ajaxForm({
			//	target: el,
			//	success: function(){Peopletab.tabs.loadAjax(null, el)}
			//}); 
			
			// Links
			$("li a, .pager a", el).not("[@events], [@$events], [@onClick]").bind("click", function (){
			  $(el).load($(this).attr("href"), function(){Peopletab.tabs.loadAjax(null, el)});
			  return false;
			});
			
			//$("li a, .pager a", el).not("[@events], [@$events], [@onClick]").each(function(i) {
		    //    var remoteURL = this.href;
		    //    var hash = '#remote-tab' + (i + 1);
		    //    this.href = hash;
		    //    $(this).click(function(e) {
		    //        var trueClick = e.clientX; // add to history only if true click occured, not a triggered click
		    //        $(el).load(remoteURL, function() {
		    //            if (trueClick) {
	        //            $.ajaxHistory.update(hash); // setting hash in callback is required to make it work in Safari
		    //            }
						
		    //            Peopletab.tabs.loadAjax(null, el);
		     //       });
        	//	});
    		//});
			
			//alert(t.href.substring(1));
			
			//$("li a, .pager a", el).not("[@events], [@$events], [@onClick]").remote(el, {}, function(){alert('testing')});
			
		
		},
		
		hideMain: function() {
			$("#mainLoad").addClass("tabs-hide");
		}
		
	},
	
	watch: function(currentUser, currentOwner) {
		var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,4}){1,3}$/;
		if (emailRegxp.test(currentUser) && emailRegxp.test(currentOwner)) {
		  currentUser = currentUser.split('@');
		  var src = 'http://' + currentUser[1] + '/' + currentUser[0] + '/User/WatchUnlock/viaRedirect?remoteOwner=' + currentOwner + '&viaFrame=true&from=' + window.location.href;
		  Peopletab.modal.initModal({
			 width: 300, 
			 height: 300,
			 modalID: 'watch',
			 content: '<iframe src="' + src + '" scrolling="no" width="100%" frameborder="0"></iframe>'}
		  );
		
		} else {
		  alert('Bad Watch Values');
		}
		

		
	},
	
	redirect: function(url) {
		window.location.href = url;
	},
	
	readMessage: function(messageID) {
		var emailRegxp = /^[0-9]+$/;
		if (emailRegxp.test(messageID)) {
		 	Peopletab.modal.initModal({
			width: 450, 
			height: 600,
			modalID: 'message' + messageID,
			ajax: $('#message' + messageID).attr("href")
		 });
		} 
	}	
		
};

$(document).ready(function(){
   Peopletab.init();
});


